package com.codeyourlife.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.codeyourlife.dao.EmailverifyDao;
import com.codeyourlife.entity.EmailVerify;

@Repository("emailDao")
public class EmailverifyDaoImpl implements EmailverifyDao {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void addRecord(EmailVerify virifyCode) {
		System.out.println("save EmailVerify");
		this.getSessionFactory().getCurrentSession().save(virifyCode);
	}
	
	@SuppressWarnings("unchecked")
	public EmailVerify getEmailVerifyByEmail(String email, String ask_type) {
		System.out.println(email+" emailDao "+ask_type);
		try{
			Query query = this.getSessionFactory().getCurrentSession().createQuery("from EmailVerify e where e.email='"+email+"' and e.send_type='"+ask_type+"'");
		    List<EmailVerify> list = query.list();
		    return list.get(0);
		}catch(Exception e){
			System.out.println("EmailDaoImpl: "+e.getStackTrace());
		}
		return null;
	}
	
	public void delRecord(int id) {
		System.out.println("delete EmailVerify");
		this.getSessionFactory().getCurrentSession().createQuery("delete EmailVerify e where e.id="+id).executeUpdate();	
	}
}
